﻿using System;
using System.Configuration;                                                                                 //包含访问配置文件所需的配置管理器；需事先在本项目的“引用”中添加对System.Configuration的引用；
using System.Data;
using System.Data.SqlClient;   
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;                                                                               //包含图像处理功能；
using System.IO;       


namespace 住院医生工作站
{
    public partial class frm_UserMessage : Form
    {
        private string PhotoFileName;
        
        public frm_UserMessage()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;                                //本窗体启动位置设为屏幕中央； 
            this.txb_No.ReadOnly = true;
            this.cmb_Department.DropDownStyle = ComboBoxStyle.DropDownList;
        
        }

        
        private void btn_Load_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
              ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;                             
            SqlCommand sqlCommand = new SqlCommand();                                                       
            SqlCommand sqlCommand2 = new SqlCommand();
            sqlCommand.Connection = sqlConnection;                                                       
            sqlCommand2.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT * FROM tb_HospitalDepartments;"; 
            sqlCommand2.CommandText = "SELECT * FROM tb_Doctort WHERE DoctorNo=@No;";
            sqlCommand2.Parameters.AddWithValue("@No", "0000000002");                                       
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable DepartmentsTable = new DataTable();                                                         
            sqlConnection.Open();
            sqlDataAdapter.Fill(DepartmentsTable);                                                                //SQL数据适配器读取数据，并填充班级数据表；
            this.cmb_Department.DataSource = DepartmentsTable;                                                         //将班级下拉框的数据源设为班级数据表；
            this.cmb_Department.DisplayMember = "Name";                                                          //将班级下拉框的显示成员设为班级数据表的名称列；
            this.cmb_Department.ValueMember = "No";                                                                 
            SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader();
            byte[] photoBytes = null;                            
            if (sqlDataReader.Read())                                                                       
            {
                this.txb_No.Text = sqlDataReader["DoctorNo"].ToString();
                this.txb_Name.Text = sqlDataReader["Name"].ToString();
                this.rdb_Male.Checked = (bool)sqlDataReader["Gender"];
                this.rdb_Female.Checked = !(bool)sqlDataReader["Gender"];
                this.dtp_BirthDate.Value = (DateTime)sqlDataReader["BirthDate"];
                this.cmb_Department.SelectedValue = (int)sqlDataReader["DepartmentNo"];
                this.txb_Phone.Text = sqlDataReader["Phone"].ToString();
                this.txb_Position.Text = sqlDataReader["Position"].ToString();
                photoBytes =
                   (sqlDataReader["Photo"] == DBNull.Value ? null : (byte[])sqlDataReader["Photo"]); 
            }
            this.textBox1.Text = this.cmb_Department.SelectedValue.ToString();
            sqlDataReader.Close();
            if (photoBytes != null)                                                                         //若学生的照片非空；
            {
                MemoryStream memoryStream = new MemoryStream(photoBytes);                                   //声明并实例化内存流，用于读取照片的字节数据；
                this.ptb_Photo.Image = Image.FromStream(memoryStream);                                      //调用图像的静态方法FromStream从内存流中读取图像，并赋予图像框；
            }
        }
      

        private void frm_UserMessage_Load(object sender, EventArgs e)
        {

        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            MemoryStream memoryStream = new MemoryStream();                                                 //声明并实例化内存流，用于读取照片的字节数据；
            this.ptb_Photo.Image.Save(memoryStream, ImageFormat.Bmp);                                       //调用图像框的图像的静态方法Save，将图像保存至内存流；
            byte[] photoBytes = new byte[memoryStream.Length];                                              //声明并实例化字节数组，用于保存照片数据；数组长度对应内存流长度；
            memoryStream.Seek(0, SeekOrigin.Begin);                                                         //保存后的内存流的偏移位置在末尾，需通过查找来将偏移位置设为起始；
            memoryStream.Read(photoBytes, 0, photoBytes.Length);   

            SqlConnection sqlConnection = new SqlConnection();                                              //声明并实例化SQL连接；
            sqlConnection.ConnectionString =
                ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;                             //配置管理器从配置文件读取连接字符串，并将之赋予SQL连接的连接字符串属性；
            SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令；
            sqlCommand.Connection = sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接；
            sqlCommand.CommandText =                                                                        //指定SQL命令的命令文本；
                "UPDATE tb_Doctort"
                + " SET Name=@Name,Gender=@Gender,BirthDate=@BirthDate,DepartmentNo=@DepartmentNo,Phone=@Phone,Photo=@Photo"
                + " WHERE DoctorNo=@No;";
            this.textBox1.Text = this.cmb_Department.SelectedValue.ToString();
            sqlCommand.Parameters.AddWithValue("@Name", this.txb_Name.Text.Trim());                         //向SQL命令的参数集合添加参数的名称、值；
            sqlCommand.Parameters.AddWithValue("@Gender", this.rdb_Male.Checked);
            sqlCommand.Parameters.AddWithValue("@BirthDate", this.dtp_BirthDate.Value);
            sqlCommand.Parameters.AddWithValue("@DepartmentNo", this.textBox1.Text.Trim ());
            sqlCommand.Parameters.AddWithValue("@Phone", this.txb_Phone.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Photo", photoBytes);
            sqlCommand.Parameters.AddWithValue("@No", "0000000002");
            sqlConnection.Open();                                                                           //打开SQL连接；
            int rowAffected = sqlCommand.ExecuteNonQuery();                                                 //调用SQL命令的方法ExecuteNonQuery来执行命令，向数据库写入数据，并返回受影响行数；
            sqlConnection.Close();                                                                          //关闭SQL连接；
            MessageBox.Show("更新" + rowAffected.ToString() + "行。");
        }

        private void btn_OpenPhoto_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog openPhotoDialog = new OpenFileDialog()                                           //声明并实例化打开文件对话框；
            {                                                                                           //在初始化器中，设置打开文件对话框的各属性；
                Title = "打开照片文件（位图格式）"                                                      //对话框标题；
                ,
                Filter = "BMP Files (*.jpg)|*.jpg"                                                    //文件格式过滤器；
                ,
                InitialDirectory = @"C:\"                                                             //初始目录；
            };
            if (openPhotoDialog.ShowDialog() == DialogResult.OK)                                            //显示打开文件对话框，若打开文件对话框的对话结果为点击OK键；
            {
                this.PhotoFileName = openPhotoDialog.FileName;                                              //将对话框获得的文件名，存入本窗体的私有字段中；
                this.ptb_Photo.Image = Image.FromFile(this.PhotoFileName);                                  //调用图像的静态方法FromFile从指定文件中读取图像，并赋予图像框；
            }
        }

     
    }
}
